package cn.fl.changeapply.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.user.facade.system.dto.SysUsrDatAuthRDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPayInfoQueryCondition;
import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.dto.CapPayInfoDTO;
import cn.fl.capital.service.ICapPayInfoApiService;
import cn.fl.changeapply.api.IPrjPrdChangeApplyApiService;
import cn.fl.changeapply.condition.PrjPrdChangeApplyQC;
import cn.fl.changeapply.constant.PrjPrdChangeApplyConstant;
import cn.fl.changeapply.dto.PrjPrdChangeApplyDTO;
import cn.fl.changeapply.service.IPrjPrdChangeApplyService;
import cn.fl.changeapply.transform.PrjPrdChangeApplyVOTransform;
import cn.fl.changeapply.vo.PrjPrdChangeApplyFlowVO;
import cn.fl.changeapply.vo.PrjPrdChangeApplyVO;
import cn.fl.commons.result.ResponseData;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.preloan.*;
import cn.fl.preloan.condition.PrjPrdDtlGpsQueryCondition;
import cn.fl.preloan.condition.PrjPrdInvoiceQueryCondition;
import cn.fl.preloan.dto.PrjPrdDtlGpsDTO;
import cn.fl.preloan.dto.PrjPrdMtgDetailRequest;
import cn.fl.preloan.dto.insurance.GetInsPlcBscInfoListRequest;
import cn.fl.preloan.dto.insurance.InsPlcBscInfoDTO;
import cn.fl.preloan.enums.PreloanResultCodeEnum;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.constant.PrjPrdVehInfoConstant;
import cn.fl.project.dto.*;
import cn.fl.project.service.IPrjPaySchDApiService;
import cn.fl.project.service.IPrjPrdPaySchMApiService;
import cn.fl.project.service.IProjectApiService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目产品变更记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjPrdChangeApplyServiceImpl implements IPrjPrdChangeApplyService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IPrjPrdChangeApplyService.class);

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Autowired
	private IPrjPrdChangeApplyApiService prjPrdChangeApplyApiService;

	@Autowired
	private ICapPayInfoApiService capPayInfoApiService;

	@Autowired
	private IFlowQueryApiService flowQueryApiService;

	@Autowired
	private IPrjPaySchDApiService prjPaySchDApiService;

	@Autowired
	private IProjectApiService projectApiService;

	@Autowired
	private IPrjPrdDtlGpsApiService prjPrdDtlGpsApiService;

	@Autowired
	private IPriorityApiService priorityApiService;

	@Autowired
	private IProjectProductMortgateService mortgateService;

	@Autowired
	private IPrjPrdVehinfoApiService prjPrdVehinfoApiService;

	@Autowired
	private IPrjPrdInvoiceApiService prjPrdInvoiceApiService;

	@Autowired
	private IInsPlcBscInfoService iInsPlcBscInfoService;

	@Autowired
	private IPrjPrdPaySchMApiService prjPrdPaySchMApiService;

	@Autowired
	private IProprdLicenseService proprdLicenseService;

	@Override
	public PrjPrdChangeApplyVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		PrjPrdChangeApplyDTO dto = this.prjPrdChangeApplyApiService.selectPrjPrdChangeApplyById(id,
				CurrentThreadContext.getCurrentSysCd(),
				CurrentThreadContext.getCurrentOrgCd());
		return PrjPrdChangeApplyVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjPrdChangeApplyVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.prjPrdChangeApplyApiService.add(PrjPrdChangeApplyVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.prjPrdChangeApplyApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PrjPrdChangeApplyVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("修改退车审批记录失败.修改对象参数不能为空！").info();
			return false;
		}
		return this.prjPrdChangeApplyApiService.modifyPrjPrdChangeApply(PrjPrdChangeApplyVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		PrjPrdChangeApplyQC condition = (PrjPrdChangeApplyQC) page.getCondition();
		if (null == condition){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询条件为空");
		}
		if (StringUtils.isNotEmpty(condition.getCstMgrId())) {
			// 不是主管能看到自己和自己下级的 主管能看见自己同级和下级的
			List<SysUsrDatAuthRDTO> list = this.priorityApiService.searchSysUsrDatAuthRWithOrgCdList(condition.getCstMgrId(),null,condition.getSysCd());
			BaseLogUtils.newLogger("当前登录人数据权限：list：{}" + JSONObject.toJSONString(list)).info();
			StringBuffer sb = new StringBuffer();
			for (SysUsrDatAuthRDTO dto : list) {
				if (sb != null && sb.length() > 0) {
					sb.append(" OR ");
				}
				if (dto.getIsChg() == 1) {//是否主管 1.是 0.否
					sb.append(" prj.CST_MGR_ORG_CD LIKE '" + dto.getOrgCd() + "'||'%' ");
				} else {
					sb.append(" prj.CST_MGR_ORG_CD LIKE '" + dto.getOrgCd() + "'||'_%' ");
				}
			}
			if (null != sb && sb.length() > 0) {
				sb.insert(0, "((");
				sb.append(") AND  ");
			} else {
				sb.insert(0, "(");
			}
			sb.append(" prj.cst_mgr_id!=" + condition.getCstMgrId() + ") OR ");
			sb.append(" prj.cst_mgr_id=" + condition.getCstMgrId());
			condition.setAccessCtrl(sb.toString());
		}
		Pager pager = this.prjPrdChangeApplyApiService.searchListPage(page);
		BaseLogUtils.newLogger("查询变更信息：page:{},pager:{}", JSONObject.toJSONString(page), JSONObject.toJSONString(pager)).info();
		if (pager != null && CollectionUtils.isNotEmpty(pager.getResultList())){
			List<PrjPrdChangeApplyVO> voList = PrjPrdChangeApplyVOTransform.toVOList((List<PrjPrdChangeApplyDTO>) pager.getResultList());
			//设置流程实例id和流程编号
			for (PrjPrdChangeApplyVO changeApplyVO : voList) {
				//设置抵押状态
				if (StringUtils.isEmpty(changeApplyVO.getGuarStsCd())) {
					changeApplyVO.setGuarStsCd("1");
				}
				//查询已还期次
				PrjPrdPaySchDQueryCondition dQC = new PrjPrdPaySchDQueryCondition();
				List<Long> paySchIds = new ArrayList<>();
				paySchIds.add(changeApplyVO.getPaySchId());
				dQC.setPaySchIdList(paySchIds);
				List<PrjPrdPaySchDDTO> paidPayPdList = this.prjPaySchDApiService.searchPaidPayPdList(dQC);
				BaseLogUtils.newLogger("查询支付表信息：paySchId:{},paidPayPdList:{}", JSONObject.toJSONString(paySchIds), JSONObject.toJSONString(paidPayPdList)).info();
				if (CollectionUtils.isNotEmpty(paidPayPdList)) {
					changeApplyVO.setPayPd(paidPayPdList.get(0).getPayPd());
				} else {
					changeApplyVO.setPayPd(0);
				}
				//查询流程信息
				SysFlowSHQueryCondition queryCondition = new SysFlowSHQueryCondition();
				queryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
				queryCondition.setDataId(changeApplyVO.getId());
				queryCondition.setWordsInTheTable(CapPayInfoConstant.CAP_FLOW_S_H_CAP);
				queryCondition.setStatus(1);
				List<SysFlowSHDTO> flowSHDTOList = flowQueryApiService.searchFlowSHList(queryCondition);
				BaseLogUtils.newLogger("查询流程信息：dataId:{},flowSHDTOList:{}", changeApplyVO.getId(), JSONObject.toJSONString(flowSHDTOList)).info();
				if (CollectionUtils.isNotEmpty(flowSHDTOList)) {
					SysFlowSHDTO sysFlowSHDTO = flowSHDTOList.get(0);
					BaseLogUtils.newLogger("查询到的流程,dataId:{},SysFlowSHDTO:{}",changeApplyVO.getId(), JSONObject.toJSONString(sysFlowSHDTO)).info();
					changeApplyVO.setProInsId(sysFlowSHDTO.getPInstId());
					changeApplyVO.setProNo(sysFlowSHDTO.getPNo());
					changeApplyVO.setAudStsCd(sysFlowSHDTO.getAudStsCd());
					if (sysFlowSHDTO.getBusTyp().equals(PrjPrdChangeApplyConstant.CODE_CHANGE_APPLY_FLOW_HC)){
						//如果是换车流程，信审通过后新车已绑定付款信息，此时则展示新车信息 上牌、抵押
						CapPayInfoQueryCondition capCondition = new CapPayInfoQueryCondition();
						capCondition.setPrjId(changeApplyVO.getPrjId());
						capCondition.setPrjPrdDtlId(changeApplyVO.getNewPrjPrdDtlId());
						capCondition.setIsDel(0);
						List<CapPayInfoDTO> capPayInfoDTOS = capPayInfoApiService.searchCapPayInfoList(capCondition);
						if (CollectionUtils.isNotEmpty(capPayInfoDTOS)){
							//上牌
							ResponseData responseData = proprdLicenseService.selectById(changeApplyVO.getNewPrjPrdDtlId());
							BaseLogUtils.newLogger("换车流程，查询上牌信息，responseData：{}" + JSONObject.toJSONString(responseData)).info();
							if (responseData.getCode().equals(ResponseData.SUCCESS_CODE) && null != responseData.getData()){
								cn.fl.preloan.dto.PrjPrdLicInfoDTO dto = (cn.fl.preloan.dto.PrjPrdLicInfoDTO) responseData.getData();
								if (null != dto){
									changeApplyVO.setLicAudStsCd(dto.getAudStsCd());
								}
							}
							//抵押
							PrjPrdMtgDetailRequest request = new PrjPrdMtgDetailRequest();
							request.setId(changeApplyVO.getNewPrjPrdDtlId());
							request.setSysCd("11000");
							request.setOrgCd("0000");
							ResponseData responseData1 = mortgateService.selectPrjPrdMtgById(request);
							BaseLogUtils.newLogger("换车流程，查询抵押信息，responseData：{}" + JSONObject.toJSONString(responseData1)).info();
							if (responseData1.getCode().equals(ResponseData.SUCCESS_CODE) && null != responseData1.getData()){
								cn.fl.preloan.dto.PrjPrdMtgDTO dto = (cn.fl.preloan.dto.PrjPrdMtgDTO) responseData1.getData();
								if (null != dto){
									changeApplyVO.setGuarStsCd(dto.getGuarStsCd());
								}
							}
						}
					}
				}
				//支付表若被迁移，租赁期次需从历史表取
				if (changeApplyVO.getLsPd() == null) {
					PrjPrdPaySchMDTO prjPrdPaySchMDTO = this.prjPrdPaySchMApiService.selectHisById(changeApplyVO.getPaySchId());
					if (prjPrdPaySchMDTO != null && prjPrdPaySchMDTO.getLsPd() != null) {
						changeApplyVO.setLsPd(prjPrdPaySchMDTO.getLsPd());
					}
				}
			}
			//设置数据字典选项
			this.setDictData(voList);
			pager.setResultList(voList);
		}
		return pager;
	}

	@Override
	public Pager searchChangeApplyListPage(Pager page) throws BaseException {
		return this.prjPrdChangeApplyApiService.searchChangeApplyListPage(page);
	}


	/**
	 * 添加数据字典名称
	 *
	 * @param list
	 */
	private void setDictData(List<PrjPrdChangeApplyVO> list) {

		if (CollectionUtils.isEmpty(list)) return;
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		//项目类型名称
		Map<String, String> prjTypCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_PRJ_TYPE_CD);
		//变更类型名称
		Map<String, String> changeTypeCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, PrjPrdChangeApplyConstant.CHANGE_TYPE_CD);
		//支付表状态名称(资产状态)
		Map<String, String> prdStsCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_PRJ_PRD_STS_CD);
		//抵押状态名称
		Map<String, String> guarStsCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_GUAR_STS_CD);
		//审核状态名称
		Map<String, String> audStsCdMap = sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapitalConstant.CODE_CAP_VF_STS_CD);

		if (CollectionUtils.isNotEmpty(list)) {
			for (PrjPrdChangeApplyVO changeApplyVO : list) {
				if (StringUtils.isNotEmpty(changeApplyVO.getPrjTypCd()) && prjTypCdMap.containsKey(changeApplyVO.getPrjTypCd())) {
					changeApplyVO.setPrjTypCdNm(prjTypCdMap.get(changeApplyVO.getPrjTypCd()));
				}
				if (StringUtils.isNotEmpty(changeApplyVO.getChangeTypeCd()) && changeTypeCdMap.containsKey(changeApplyVO.getChangeTypeCd())) {
					changeApplyVO.setChangeTypeCdNm(changeTypeCdMap.get(changeApplyVO.getChangeTypeCd()));
				}
				if (StringUtils.isNotEmpty(changeApplyVO.getPrdStsCd()) && prdStsCdMap.containsKey(changeApplyVO.getPrdStsCd())) {
					changeApplyVO.setPrdStsCdNm(prdStsCdMap.get(changeApplyVO.getPrdStsCd()));
				}
				if (StringUtils.isNotEmpty(changeApplyVO.getGuarStsCd()) && guarStsCdMap.containsKey(changeApplyVO.getGuarStsCd())) {
					changeApplyVO.setGuarStsCdNm(guarStsCdMap.get(changeApplyVO.getGuarStsCd()));
				}
				if (StringUtils.isNotEmpty(changeApplyVO.getAudStsCd()) && audStsCdMap.containsKey(changeApplyVO.getAudStsCd())) {
					changeApplyVO.setAudStsCdNm(audStsCdMap.get(changeApplyVO.getAudStsCd()));
				}
			}
		}

		//事业部组织架构
		List<SysOrgDTO> sysOrgDTOList = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
		Map<String, SysOrgDTO> buOrgCdMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(sysOrgDTOList)) {
			for (SysOrgDTO sysOrgDTO : sysOrgDTOList) {
				buOrgCdMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO);
			}
		}
		SysOrgDTO sysOrgDTO = null;
		String sysOrgCd = null;
		for (PrjPrdChangeApplyVO vo : list) {
			if (buOrgCdMap.containsKey(vo.getBuOrgCd())) {
				sysOrgDTO = buOrgCdMap.get(vo.getBuOrgCd());
				vo.setBuOrgCdNm(sysOrgDTO.getOrgNm());
			}
			if (buOrgCdMap.containsKey(vo.getCstMgrOrgCd())) {
				sysOrgDTO = buOrgCdMap.get(vo.getCstMgrOrgCd());
				sysOrgCd = sysOrgDTO.getOrgCd();
				while(sysOrgCd.length()>8){
					if("4".equals(sysOrgDTO.getOrgTypCd())){//分公司
						vo.setCyOrgCdNm(sysOrgDTO.getOrgNm());
					}else if("3".equals(sysOrgDTO.getOrgTypCd())){
						vo.setPrOrgCdNm(sysOrgDTO.getOrgNm());
						break;
					}
					sysOrgCd = sysOrgCd.substring(0,sysOrgCd.length()-4);
					if(buOrgCdMap.containsKey(sysOrgCd)){
						sysOrgDTO = buOrgCdMap.get(sysOrgCd);
					}else{
						break;
					}
				}
			}
		}
	}

	@Override
	public List<KeyValueVO> getKeyValueList(String code, String sysCd, String orgCd) throws BaseException {
		List<KeyValueVO> keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApiService
				.searchGroup(sysCd, orgCd, code);
		if (CollectionUtils.isNotEmpty(dictionaryList)) {
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}

	@Override
	public List<KeyValueVO> searchBusinessOrgList() throws BaseException {
		List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
		List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
		if (CollectionUtils.isEmpty(list)) {
			return kvList;
		}
		for (SysOrgDTO sysOrgDTO : list) {
			KeyValueVO kv = new KeyValueVO();
			kv.setKey(sysOrgDTO.getOrgCd());
			kv.setValue(sysOrgDTO.getOrgNm());
			kvList.add(kv);
		}
		return kvList;
	}

	@Override
	public void changeCheck(Long prjId, Long paySchId, String changeTypeCd) throws BaseException {
		this.prjPrdChangeApplyApiService.changeCheck(prjId, paySchId, changeTypeCd);
	}

	@Override
	public PrjPrdChangeApplyFlowVO getPrdChangeDetail(PrjPrdChangeApplyQC qc, boolean delFlag) throws BaseException {
		Long prjPrdDtlId = qc.getPrjPrdDtlId();
		if (prjPrdDtlId == null) {
			BaseLogUtils.newLogger("getPrdChangeDetail.换车申请页面获取详细信息ID为空!" + CurrentThreadContext.getCurrentUserName()).info();
			return null;
		}
		BaseLogUtils.newLogger("getPrdChangeDetail.Start.dtlId:{},qc:{},userNm:{}", prjPrdDtlId, JSON.toJSONString(qc), CurrentThreadContext.getCurrentUserName()).info();
		PrjPrdChangeApplyFlowVO vo = new PrjPrdChangeApplyFlowVO();
		vo.setDtlId(prjPrdDtlId);
		//项目产品明细信息表
		PrjPrdDtlDTO prjPrdDtlDTO = this.projectApiService.selectPrjPrdDtlById(prjPrdDtlId);
		if (prjPrdDtlDTO != null) {
			vo.setPrjPrdId(prjPrdDtlDTO.getPrjPrdId());
			vo.setLnkCompNm(prjPrdDtlDTO.getLnkCompNm());
			vo.setVin(prjPrdDtlDTO.getVin());
			if (prjPrdDtlDTO.getPrjPrdId() != null) {
				//项目产品基础信息表
				PrjPrdBscInfoDTO prjPrdBscInfoDTO = this.projectApiService.selectPrjPrdBscInfoById(prjPrdDtlDTO.getPrjPrdId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				if (prjPrdBscInfoDTO != null) {
					vo.setPrdNm(prjPrdBscInfoDTO.getPrdNm());
					vo.setBrand(prjPrdBscInfoDTO.getBrCd());
					vo.setMfrNm(prjPrdBscInfoDTO.getMfrNm());
					vo.setSplNm(prjPrdBscInfoDTO.getSplNm());
					//新车租赁物价格
					vo.setPrdAmt(prjPrdBscInfoDTO.getPrdAmt() == null ? null : prjPrdBscInfoDTO.getPrdAmt().longValue());
					//项目卡车产品信息表
					PrjPrdExtDTO prjPrdExtDTO = this.projectApiService.selectPrjPrdExtById(prjPrdBscInfoDTO.getId());
					if (prjPrdExtDTO != null) {
						vo.setHp(prjPrdExtDTO.getHp());
					}
				}
			}
			vo.setActPrdId(prjPrdDtlDTO.getActPrdId());
			//发动机号
			vo.setEngineNo(prjPrdDtlDTO.getEngNo());
			//发动机型号
			vo.setEngMdl(prjPrdDtlDTO.getEngMdl());
			if (prjPrdDtlDTO.getActPrdId() != null) {
				//合格证
				ResponseData vehResp = this.prjPrdVehinfoApiService.selectById(prjPrdDtlDTO.getActPrdId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				BaseLogUtils.newLogger("getPrdChangeDetail.换车申请页面查询合格证.actPrdId:{},resp:{},userNm:{}", prjPrdDtlDTO.getActPrdId(), JSON.toJSONString(vehResp), CurrentThreadContext.getCurrentUserName()).info();
				if (vehResp != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(vehResp.getCode()) && vehResp.getData() != null) {
					cn.fl.preloan.dto.PrjPrdVehInfoDTO vehInfoDTO = (cn.fl.preloan.dto.PrjPrdVehInfoDTO) vehResp.getData();
					if (null != vehInfoDTO) {
						vo.setCertNo(vehInfoDTO.getCertNo());
					}
				}
			}
			// 获取GPS卡号
			PrjPrdDtlGpsQueryCondition gpsQC = new PrjPrdDtlGpsQueryCondition();
			gpsQC.setPrjDtlId(prjPrdDtlId);
			BaseLogUtils.newLogger("getPrdChangeDetail.换车申请页面查询GPS!prjPrdDtlId:{},userNm:{}", prjPrdDtlId, CurrentThreadContext.getCurrentUserName()).info();
			ResponseData responseData = this.prjPrdDtlGpsApiService.selectList(gpsQC);
			BaseLogUtils.newLogger("getPrdChangeDetail.换车申请页面查询GPS!prjPrdDtlId:{},resp:{},userNm:{}", prjPrdDtlId, JSON.toJSONString(responseData), CurrentThreadContext.getCurrentUserName()).info();
			List<PrjPrdDtlGpsDTO> gpsDTOList = null;
			if (responseData != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(responseData.getCode()) && responseData.getData() != null) {
				gpsDTOList = (List<PrjPrdDtlGpsDTO>) responseData.getData();
			}
			if (delFlag && CollectionUtils.isEmpty(gpsDTOList)) {
				//查询删除状态的GPS
				gpsQC.setIsDel(1);
				ResponseData data = this.prjPrdDtlGpsApiService.selectList(gpsQC);
				BaseLogUtils.newLogger("getPrdChangeDetail.换车申请页面查询GPS2!prjPrdDtlId:{},resp:{},userNm:{}", prjPrdDtlId, JSON.toJSONString(data), CurrentThreadContext.getCurrentUserName()).info();
				if (data != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(data.getCode()) && data.getData() != null) {
					gpsDTOList = (List<PrjPrdDtlGpsDTO>) data.getData();
				}
			}
			if (CollectionUtils.isNotEmpty(gpsDTOList)) {
				for (PrjPrdDtlGpsDTO dto : gpsDTOList) {
					if (null != dto.getSerialNo()) {
						if (dto.getSerialNo() == 1) {
							vo.setGpsCardNo1((null == dto.getGpsCardNo()) ? "" : dto.getGpsCardNo());
						} else if (dto.getSerialNo() == 2) {
							vo.setGpsCardNo2((null == dto.getGpsCardNo()) ? "" : dto.getGpsCardNo());
						} else if (dto.getSerialNo() == 3) {
							vo.setGpsCardNo3((null == dto.getGpsCardNo()) ? "" : dto.getGpsCardNo());
						}
					}
				}
			}

			//上牌状态
			String licAudStsCdNm = "未上牌";
			String isExistSp = "0";
			ResponseData licInfoResp = proprdLicenseService.selectById(prjPrdDtlId);
			BaseLogUtils.newLogger("===换车申请查询上牌信息End===prjPrdDtlId:{},licInfoResp:{}", prjPrdDtlId, JSON.toJSONString(licInfoResp)).info();
			if (licInfoResp != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(licInfoResp.getCode())) {
				cn.fl.preloan.dto.PrjPrdLicInfoDTO licInfoDTO = (cn.fl.preloan.dto.PrjPrdLicInfoDTO) licInfoResp.getData();
				if (null != licInfoDTO && licInfoDTO.getId() != null){
					isExistSp = "1";
					if (FlowConstant.FLOW_CHECK_STATE_PASS.equals(licInfoDTO.getAudStsCd())) {
						licAudStsCdNm = "已上牌";
					}
				}
			}
			vo.setLicAudStsCdNm(licAudStsCdNm);
			vo.setIsExistSp(isExistSp);
			//抵押状态
			String guarStsCdNm = "未抵押";
			String isExistDy = "0";
			PrjPrdMtgDetailRequest request = new PrjPrdMtgDetailRequest();
			request.setId(prjPrdDtlId);
			request.setSysCd(CurrentThreadContext.getCurrentSysCd());
			request.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			ResponseData response = this.mortgateService.selectPrjPrdMtgById(request);
			BaseLogUtils.newLogger("===换车申请查询抵押信息End===prjPrdDtlId:{},resp:{}", prjPrdDtlId, JSON.toJSONString(response)).info();
			if (response != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(response.getCode())) {
				cn.fl.preloan.dto.PrjPrdMtgDTO prjPrdMtgDTO = (cn.fl.preloan.dto.PrjPrdMtgDTO) response.getData();
				if (null != prjPrdMtgDTO && prjPrdMtgDTO.getId() != null) {
					isExistDy = "1";
					if (StringUtils.isNotEmpty(prjPrdMtgDTO.getGuarStsCd())) {
						//抵押状态
						Map<String, String> guarStsMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GUAR_STS_CD);
						if (guarStsMap != null && guarStsMap.containsKey(prjPrdMtgDTO.getGuarStsCd())) {
							guarStsCdNm = guarStsMap.get(prjPrdMtgDTO.getGuarStsCd());
						}
					}
				}
			}
			vo.setGuarStsCdNm(guarStsCdNm);
			vo.setIsExistDy(isExistDy);
			//发票状态
			String invoiceStsCd = "";
			String isExistinvoice = "0";
			PrjPrdInvoiceQueryCondition invoiceQueryCondition = new PrjPrdInvoiceQueryCondition();
			invoiceQueryCondition.setPrjPrdDtlId(prjPrdDtlId);
			BaseLogUtils.newLogger("=换车申请查询发票信息Start=prjPrdDtlId:{},qc:{}", prjPrdDtlId, JSON.toJSONString(invoiceQueryCondition)).info();
			ResponseData<List<cn.fl.preloan.dto.PrjPrdInvoiceDTO>> invoiceResp = this.prjPrdInvoiceApiService.selectListByCondition(invoiceQueryCondition);
			BaseLogUtils.newLogger("=换车申请查询发票信息End=prjPrdDtlId:{},resp:{}", prjPrdDtlId, JSON.toJSONString(invoiceResp)).info();
			List<cn.fl.preloan.dto.PrjPrdInvoiceDTO> invoiceDTOS = null;
			if (invoiceResp != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(invoiceResp.getCode())) {
				invoiceDTOS = invoiceResp.getData();
			}
			if (delFlag && CollectionUtils.isEmpty(invoiceDTOS)) {
				//查询删除状态的发票
				invoiceQueryCondition.setIsDel(1);
				ResponseData<List<cn.fl.preloan.dto.PrjPrdInvoiceDTO>> invoiceResp2 = this.prjPrdInvoiceApiService.selectListByCondition(invoiceQueryCondition);
				BaseLogUtils.newLogger("=换车申请查询发票信息End2=prjPrdDtlId:{},resp:{}", prjPrdDtlId, JSON.toJSONString(invoiceResp2)).info();
				if (invoiceResp2 != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(invoiceResp2.getCode())) {
					invoiceDTOS = invoiceResp2.getData();
				}
			}
			if (CollectionUtils.isNotEmpty(invoiceDTOS)) {
				isExistinvoice = "1";
				for (cn.fl.preloan.dto.PrjPrdInvoiceDTO dto : invoiceDTOS) {
					if (dto.getFlag() != null && "10".equals(dto.getFlag().toString())) {
						Map<String, String> invoiceStsMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), PrjPrdVehInfoConstant.PRJ_VEH_AUD_STS_CD);
						invoiceStsCd = invoiceStsMap.get(dto.getAudStsCd());
						break;
					}
				}
			}
			vo.setInvoiceStsCd(invoiceStsCd);
			vo.setIsExistInvoice(isExistinvoice);
			//保单状态
			cn.fl.preloan.condition.InsPlcBscInfoQueryCondition condition = new cn.fl.preloan.condition.InsPlcBscInfoQueryCondition();
			condition.setPrjPrdDtlId(prjPrdDtlId);
			condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
			condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
			List<String> insTypCdList=new ArrayList<>();
			//商业险
			insTypCdList.add("1");
			//交强险
			insTypCdList.add("2");
			condition.setInsTypCdList(insTypCdList);
			GetInsPlcBscInfoListRequest listRequest = new GetInsPlcBscInfoListRequest(condition);
			BaseLogUtils.newLogger("=换车申请查询保单信息Start=prjPrdDtlId:{},listRequest:{}", prjPrdDtlId, JSON.toJSONString(listRequest)).info();
			ResponseData insPlcResp = iInsPlcBscInfoService.searchList(listRequest);
			BaseLogUtils.newLogger("=换车申请查询保单信息End=prjPrdDtlId:{},resp:{}", prjPrdDtlId, JSON.toJSONString(insPlcResp)).info();
			List<InsPlcBscInfoDTO> insPlcList = null;
			if (insPlcResp != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(insPlcResp.getCode())
					&& insPlcResp.getData() != null) {
				insPlcList = (List<InsPlcBscInfoDTO>) insPlcResp.getData();
			}
			if (delFlag && CollectionUtils.isEmpty(insPlcList)) {
				//查询删除状态的保单
				condition.setIsDel(1);
				ResponseData insPlcResp2 = iInsPlcBscInfoService.searchList(listRequest);
				BaseLogUtils.newLogger("=换车申请查询保单信息End2=prjPrdDtlId:{},resp:{}", prjPrdDtlId, JSON.toJSONString(insPlcResp2)).info();
				if (insPlcResp2 != null && PreloanResultCodeEnum.SUCCESS.getCode().equals(insPlcResp2.getCode())
						&& insPlcResp2.getData() != null) {
					insPlcList = (List<InsPlcBscInfoDTO>) insPlcResp2.getData();
				}
			}
			if (CollectionUtils.isNotEmpty(insPlcList)) {
				for (InsPlcBscInfoDTO insPlcBscInfoDTO : insPlcList) {
					if ("1".equals(insPlcBscInfoDTO.getInsTypCd())) {
						if (vo.getCommercialPlcId() == null) {
							vo.setCommercialPlcId(insPlcBscInfoDTO.getId());
							vo.setCommercialPlcStsCd(insPlcBscInfoDTO.getPlcStsCd());
							vo.setCommercialPlcStsCdNm(insPlcBscInfoDTO.getPlcStsCdNm());
						}
					} else {
						if (vo.getInsPlcId() == null) {
							vo.setInsPlcId(insPlcBscInfoDTO.getId());
							vo.setPlcStsCd(insPlcBscInfoDTO.getPlcStsCd());
							vo.setPlcStsCdNm(insPlcBscInfoDTO.getPlcStsCdNm());
						}
					}
				}
			}
			// 租赁物价格
			if (qc.getPaySchId() != null) {
				PrjPrdPaySchMDTO paySchMDTO = this.prjPrdPaySchMApiService.selectPrjPrdPaySchMById(qc.getPaySchId());
				if (paySchMDTO != null) {
					vo.setTotLsItmAmt(paySchMDTO.getTotLsItmAmt() == null ? 0 : paySchMDTO.getTotLsItmAmt().longValue());
				}
			}
		}
		BaseLogUtils.newLogger("getPrdChangeDetail.End.dtlId:{},vo:{},userNm:{}", prjPrdDtlId, JSON.toJSONString(vo), CurrentThreadContext.getCurrentUserName()).info();
		return vo;
	}

	@Override
	public List<PrjPrdChangeApplyVO> selectList(PrjPrdChangeApplyQC qc) throws BaseException {
		BaseLogUtils.newLogger("selectList.查询车辆变更信息!qc:{},userNm:{}", JSON.toJSONString(qc), CurrentThreadContext.getCurrentUserName()).info();
		if (qc == null) {
			return null;
		}
		List<PrjPrdChangeApplyDTO> dtoList = this.prjPrdChangeApplyApiService.selectList(qc,
				CurrentThreadContext.getCurrentSysCd(),
				CurrentThreadContext.getCurrentOrgCd());
		BaseLogUtils.newLogger("selectList.查询车辆变更信息!End.size:{}", dtoList == null ? 0 : dtoList.size()).info();
		return PrjPrdChangeApplyVOTransform.toVOList(dtoList);
	}
}
